package com.dragon.client.controller.supplies;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.supplies.IWarehouseService;
import com.dragon.repository.domain.dto.WarehouseDTO;
import com.dragon.repository.domain.vo.WarehouseVO;
import com.dragon.repository.entity.Warehouse;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 仓库表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-11-13
 */
@RestController
@RequestMapping("warehouse")
@Api(tags = "仓库")
public class WarehouseController {

    @Autowired
    IWarehouseService service;

    @GetMapping("list")
    @ApiOperation(value = "仓库列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, type = "string")
    })
    public ResponseData<List<WarehouseDTO>> warehouseList(@RequestParam Integer page,
                                                          @RequestParam Integer limit,
                                                          @RequestParam(required = false) String search){
        Page<Warehouse> warehousePage =service.warehouseList(page, limit, search);
        return ResponseData.success(WarehouseDTO.convertList(warehousePage.getRecords()), warehousePage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    public ResponseData addWarehouse(@RequestBody WarehouseVO vo) {
        service.addWarehouse(WarehouseVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public ResponseData updateWarehouse(@RequestBody WarehouseVO vo){
        service.updateWarehouse(WarehouseVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("detailById")
    @ApiOperation(value = "根据id查详情")
    public ResponseData<WarehouseDTO> detailById(@ApiParam(name = "id", value = "id", required = true, type = "int")
                                                 @RequestParam Integer id){
        Warehouse warehouse = service.detailById(id);
        return ResponseData.success(WarehouseDTO.convert(warehouse));
    }
}
